package org.pku_uestc.hackathon.sensor.mobile;

import org.pku_uestc.hackathon.SilentActivity.WhiteListInterface;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;

public class CallReceiver extends BroadcastReceiver {
	
	public static final String TAG = "CallReceiver";
	
	private static final String INCOMING_CALL_ACTION = TelephonyManager.ACTION_PHONE_STATE_CHANGED;

	private WhiteListInterface whitelist;
	
	private String[] phones;
	
	public String getAction() {
		return INCOMING_CALL_ACTION;
	}
	
	public void setPhones(String[] phones, WhiteListInterface whitelist) {
		this.phones = phones;
		this.whitelist = whitelist;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.i("HHH", action);
		if (INCOMING_CALL_ACTION.equals(action)) {
			int length = phones.length;
			String phone = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
			Log.i("HHH", phone);
			for(int i = 0; i < length; i++) {
				if(phone.equals(phones[i])) {
					whitelist.onPass(WhiteListInterface.TYPE_CALL);
					break;
				}
			}
		}
	}
	
}
